using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Web;

namespace ASPNETVN.PORTAL.Components.Helpers
{
    public enum ImageResizeOption
    {
        NoResize,
        FixedWidthHeight,
        MaintainAspectWidth,
        MaintainAspectHeight
    }

    public class ImageHelper
    {
        public static string GetUserImage(object status)
        {
            if (status.ToString().ToUpper() == "DEACTIVE")
            {
                return "~/Images/Icons/User_Deactive.gif";
            }
            return "~/Images/Icons/User.gif";
        }

        //-----------------------------------------------------------------------------------------

        public static string BuildRateImage(object rate, HttpRequest request)
        {
            string returnValue = string.Empty;
            for (int i = 1; i <= 10; i++)
            {
                if (i <= Convert.ToInt32(rate))
                {
                    if (i%2 == 0)
                    {
                        returnValue += string.Format("<img src='{0}/Images/Icons/Rate/star-right-on.gif' border='0'/>", Global.GetApplicationPath(request));
                    }
                    else
                    {
                        returnValue += string.Format("<img src='{0}/Images/Icons/Rate/star-left-on.gif' border='0'/>", Global.GetApplicationPath(request));
                    }
                }
                else
                {
                    if (i%2 == 0)
                    {
                        returnValue += string.Format("<img src='{0}/Images/Icons/Rate/star-right-off.gif' border='0'/>", Global.GetApplicationPath(request));
                    }
                    else
                    {
                        returnValue += string.Format("<img src='{0}/Images/Icons/Rate/star-left-off.gif' border='0'/>", Global.GetApplicationPath(request));
                    }
                }
            }
            return returnValue;
        }

        //-----------------------------------------------------------------------------------------

        public static Bitmap ResizeImage(Image original, int newWidth, int newHeight, ImageResizeOption option)
        {
            if (original.Width == 0 || original.Height == 0 || newWidth == 0 || newHeight == 0) return null;
            if (original.Width < newWidth) newWidth = original.Width;
            if (original.Height < newHeight) newHeight = original.Height;

            switch (option)
            {
                case ImageResizeOption.NoResize:
                    newWidth = original.Width;
                    newHeight = original.Height;
                    break;
                case ImageResizeOption.FixedWidthHeight:
                    break;
                case ImageResizeOption.MaintainAspectWidth:
                    newHeight = (newWidth * original.Height / original.Width);
                    break;
                case ImageResizeOption.MaintainAspectHeight:
                    newWidth = (newHeight * original.Width / original.Height);
                    break;
            }

            Bitmap newBitmap = new Bitmap(newWidth, newHeight);
            Graphics g = Graphics.FromImage(newBitmap);
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.DrawImage(original, 0, 0, newWidth, newHeight);
            return newBitmap;
        }
    }
}